#ifndef DATA_STRUCTURE_CTRLUTIL_H
#define DATA_STRUCTURE_CTRLUTIL_H

#include "SortTool.h"
#include "BSTree.h"
#include <ctime>
#include <cstdlib>
#include "vector"

class CtrlUtil {
public:
    CtrlUtil *start() {
        while (true) {
            cout << "(1-2)Which question to test? :";
            int i;
            cin >> i;
            switch (i) {
                case 1:
                    cout << "(question1)" << endl;
                    q1();
                    break;
                case 2:
                    cout << "(question2)" << endl;
                    q2();
                    break;
                default:
                    return this;
            }
            cout << "-------------" << endl;
        }
    }

    void q1() {
        cout << "输入个数，最小值，最大值:";
        int n, min, max;
        cin >> n >> min >> max;
        for (int i = 1; i <= 2; ++i) {
            vector<int> arr1;
            vector<int> arr2;
            for (int i = 0; i < n; ++i) {
                int a = rand() % (max - min + 1) + min;
                arr1.push_back(a);
                arr2.push_back(a);
            }
            cout << "生成数据" << i << ":";
            printArr(arr1);
            int compareNum = 0, moveNum = 0;
            quickSort(arr1, compareNum, moveNum);
            cout << "快速排序后:";
            printArr(arr1);
            cout << "比较次数:" << compareNum << " 移动次数:" << moveNum << endl;

            compareNum = 0, moveNum = 0;
            shell(arr2, compareNum, moveNum);
            cout << "希尔排序后:";
            printArr(arr2);
            cout << "比较次数:" << compareNum << " 移动次数:" << moveNum << endl;
            cout << endl;
        }

    }

    void q2() {
        BSTree tree = NULL;
        BSTree tree1 = NULL;
        int k;
        while (true) {
            cout << "(0-5):1—建立 2—输出 3—插入 4—查找 5—删除 0—退出:";
            int i;
            cin >> i;
            switch (i) {
                case 1:
                    cout << "建立" << endl;
                    CreatBST(tree);
                    break;
                case 2:
                    cout << "输出" << endl;
                    InorderTree(tree);
                    break;
                case 3:
                    cout << "插入" << endl;
                    cout << "输入待插入值:";
                    ElemType e;
                    cin >> e.key;
                    InsertBST(tree, e);
                    InorderTree(tree);
                    break;
                case 4:
                    cout << "查找" << endl;
                    k = 0;
                    cout << "输入要查询的元素:" << endl;
                    cin >> k;
                    int a;
                    a = 0;
                    tree1 = SearchBST(tree, k, a);
                    if (tree1) cout << "存在此元素" << endl;
                    else cout << "不存在此元素" << endl;
                    cout << "比较次数:" << a << endl;
                    break;
                case 5:
                    cout << "删除" << endl;
                    k = 0;
                    cout << "输入要删除的元素:" << endl;
                    cin >> k;
                    DeleteBST(tree, k);
                    InorderTree(tree);
                    break;
                default:
                    return;
            }
            cout << "-------------" << endl;
        }

    }
};

#endif //DATA_STRUCTURE_CTRLUTIL_H
